layui.define(function (exports) {
	var object = {
			c: {data:[]}, // 配置项
			f: function (c) { // 数据初始化
				var s=new Date().getTime(),t = this;
				t.c.id = c.id?c.id:'simple_table_base_info';
				t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
				//c.data = JSON.parse('[{"accountName":"费玉清","timeTypeName":"00:00~00:59","operatorName":"周杰伦"}]');
				t.c.data = c.data; // 数据
				t.c.dictKey = c.dictKey;
			},
	        load:function () { // 加载控件
	        	var h = ''
	        		+ '<div class="layui-form layui-form-item" lay-filter="' + this.id + '">'
		        		+ '<div class="layui-col-md12">'
			        		+ '<div class="layui-input-block">'
			        			+ '<button type="button" class="layui-btn layui-btn-primary btn-add-account_away"><i class="layui-icon layui-icon-addition"></i> 新增字典</button>'
			        		+ '</div>'
		        		+ '</div>'
	        		+ '</div>'
	            	+ '<div class="layui-form-item" pane>'
	            		+ '<label class="layui-form-label">字典列表</label>'
	            		+ '<div class="layui-input-block" id="'+this.c.tableId+'">'
	            			+ this.loadTable()
	            		+ '</div>'
	        		+ '</div>'
        		+ '</div>';
	            $('#'+this.c.id).html(h);
	            this.listenBtn();
	        },
	        loadTable:function () { // 加载表格
	            return '<table class="layui-table" style="width: 98%;" lay-skin="" style="margin:0"><thead>'+this.loadTableOfThead()+'</thead><tbody>'+this.loadTableOfTbody()+'</tbody></table>';
	        },
	        loadTableOfThead:function () { // 加载表头
	            var h = '<thead><tr>';
	            h += '<th style="width:8%; text-align: center;">字典名称</th>';
	            h += '<th style="width:8%; text-align: center;">字典值</th>';
	            h+= '<th style="width:15%; text-align: center;">操作</th></tr></thead>';
				return h;
	        },
	        loadTableOfTbody:function () { // 加载表tbody
	        	var t = this;
	        	var h = '<tbody>';
	            for(var i=0;i<t.c.data.length;i++){
		            h += '<tr>';
		            h += '<td style="text-align:center;">' + t.c.data[i].dictName + '</td>';
		            h += '<td style="text-align:center;">' + t.c.data[i].dictValue + '</td>';
	                h += '<td style="text-align:center;"><a class="layui-btn layui-btn-danger layui-btn-sm" data-index="'+i+'" lay-filter="remove"><i class="layui-icon layui-icon-delete"></i>移除</a><a class="layui-btn layui-btn-sm" id="id_' + new Date().getTime() + '_' + i + '" data-index="'+i+'" lay-filter="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a></td>'
	                h+= '</tr>';
	            }
                
	            h += '</tbody>';
				return h;
	        },
	        listenBtn:function () { // 添加监听
	            var t = this;
	            // 监听“配置字典KEY”按钮
	            $('#' + t.c.id).find('.btn-add-account_away').on('click',function (e) {
		            
	            	var w=($(window).width()*0.9);
	            	var h=($(window).height() - 50);
	                layer.open({
	                    type: 2,
	                    area: [w+'px', h +'px'],
	                    fix: true,
	                    maxmin: false,
	                    shadeClose: true,
	                    shade:0.4,
	                    title: "新增字典",
	                    content: '/web/sys/ysDict/toAdd?dictKey=' + t.c.dictKey
	                });
	            });
	            // 监听“编辑”
	            $('#' + t.c.tableId).find('a[lay-filter="edit"]').on('click',function (e) {
	                var i = $(this).attr('data-index');
	            	var w=($(window).width()*0.9);
	            	var h=($(window).height() - 50);
	                layer.open({
	                    type: 2,
	                    area: [w+'px', h +'px'],
	                    fix: true,
	                    maxmin: false,
	                    shadeClose: true,
	                    shade:0.4,
	                    title: "修改字典",
	                    content: '/web/sys/ysDict/toEdit?id=' + t.c.data[i]['id']
	                });
	            });
	            $('#' + t.c.tableId).find('a[lay-filter="remove"]').on('click',function (e) {
	                var i = $(this).attr('data-index');
	                
					layui.layer.confirm("删除后不可恢复，确定彻底删除？",{
						btn: ['确定', '取消']
					}, function (index) {
						$.ajax({
							type: "POST",
							url: '/api/inter/ysDict/delete',
							data: {id: t.c.data[i]['id']},
							success: function (data) {
								if (data.code == 0) {
									layer.msg(data.msg);
									setTimeout(function(){
										location.reload();
									}, 1000);
								} else {
									alert(data.msg);
								}
							}
						});
						layer.close(index);
					});
	            });
	        },
	        reloadTable:function () { // 重新加载表格
	            $('#'+this.c.tableId).html(this.loadTable());
	            this.listenBtn();
	        },
	        getData:function () {
	            return this.c.data;
	        },
	        init:function (c) {this.f(c);this.load();return object;},
	        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
	        addRow:function (row) {
	        	var c = this.c;
	        	c.data.push(row);
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        addRows:function (rows) {
	        	var c = this.c;
	        	for (var i = 0; i < rows.length; i++) {
		        	c.data.push(rows[i]);
				}
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        editRow:function (index, row) {
	        	var c = this.c;
				c.data[index] = row;
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        editRowOfAttr:function (index, row) {
	        	var c = this.c;
	        	for(var key in row){
	        		c.data[index][key] = row[key]
	        	}
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        reloadRows:function (rows) {
	        	var c = this.c;
	        	c.data = rows;
	        	return this.reload(c);
	        }
		};
	exports('simple_table_base_info', object);
});